﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using StructureMap;


namespace MyMovieInfo
{
    class Program
    {
        private static List<string> _fileList;
        private static string _outputPath;

        static void Main(string[] args)
        {
            //command line arguments
            string movieFilePath = ArgumentUtils.GetArgument(args, ArgumentUtils.Args.MovieFilePath);
            _outputPath = ArgumentUtils.GetArgument(args, ArgumentUtils.Args.OutputPath);

            //read file names from file system
            _fileList = FileUtils.GetListFromFileSystem(movieFilePath, ".avi");
            
            if (_fileList.Count > 0)
            {
                //hook up the dependencies using an IoC Container
                IContainer container = ConfigureDependencies();                
                
                //IOC - the dependencies of the contructor are handled by the IoC container.
                IMovieResolution resolver = container.GetInstance<IMovieResolution>();

                resolver.ExecuteQueries();
                resolver.SaveResults();

                #region Display to the console
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Found Movies");
                resolver.Movies.Where(p => p.MatchFound == true).ToList().ForEach(e => PrintMovieToConsole(e));

                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Movies Not Found");
                resolver.Movies.Where(p => p.MatchFound == false).ToList().ForEach(e => PrintMovieToConsole(e));
                #endregion

            }
            else
            {
                Console.WriteLine(String.Format("No files found in \"{0}\".", movieFilePath));
            }

            Console.ReadKey();
        }

        private static IContainer ConfigureDependencies()
        {
            return new Container(container =>
            {                

                container.For<IMovieResolution>().Use<MovieResolver>();

                container.For<IMovieListCreator>()
                    .Use<MovieListFromFileNames>()
                    .Ctor<List<string>>("filenames")
                    .Is(_fileList);

                container.For<IPersistenceService>()
                   .Use<XMLPersistenceService>()
                   .Ctor<string>("filename")
                   .Is(Path.Combine(_outputPath, "movies-IoC.xml"));

                container.For<IMovieLookup>().Add<NetflixLookup>();
                container.For<IMovieLookup>().Add<IMDBLookup>();
                container.For<IMovieLookup>().Add<TMDBLookup>();
                //add another lookup service.

                //use the app.config file
                //container.IncludeConfigurationFromConfigFile = false;
            });
        }

        #region Pretty Console Printer
        private static void PrintMovieToConsole(Movie movie)
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("{0} - {1}", movie.Name, movie.Year);
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("{0}\n{1}\n{2}\nSource:{3}\n\n", movie.Synopsis, movie.CoverURL, movie.FilePath, movie.Source);
        }
        #endregion
    }

}
